package cn.com.fuhuitong.main.mine.ui.activity

import android.app.AlertDialog
import android.view.View
import android.widget.EditText
import androidx.lifecycle.Observer
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BaseApplication
import cn.com.fuhuitong.aroute.ARouteConstance
import cn.com.fuhuitong.http.HttpResponseStatus
import cn.com.fuhuitong.main.access.entity.UserInfoEntity
import cn.com.fuhuitong.main.base.ViewModeActivity
import cn.com.fuhuitong.main.mine.vm.ProfileViewModel
import com.alibaba.android.arouter.facade.annotation.Route
import kotlinx.android.synthetic.main.activity_mine_profile.*


@Route(path = ARouteConstance.ACTIVITY_URL_MINE_PROFILE)
class MineProfileActivity : ViewModeActivity<ProfileViewModel>() {

    private var nickname = "森林狂想"

    override fun viewModel(): ProfileViewModel = createViewModel()

    override val layoutResId: Int
        get() = R.layout.activity_mine_profile

    override fun initView() {

        image_profile_back.setOnClickListener {
            this.finish()
        }

        val userInfo: UserInfoEntity? = BaseApplication.userInfo()
        text_profile_user_name.text = userInfo?.nickname
        text_profile_user_mobile.text = userInfo?.mobile
        text_profile_user_wx_code.text = userInfo?.username

        frame_profile_user_name.setOnClickListener {
            val dialog: AlertDialog.Builder = AlertDialog.Builder(this)
            val view: View = View.inflate(this, R.layout.dialog_profile_nickname, null)
            val editNickname: EditText = view.findViewById(R.id.edit_profile_change_nickname)
            dialog.setTitle("修改姓名")
            dialog.setCancelable(false)
            dialog.setView(view)
            dialog.setPositiveButton("确定") { dialog, which ->
                dialog.cancel()
                if (editNickname.text.isEmpty()) {
                    return@setPositiveButton
                }
                nickname = editNickname.text.toString()
                viewModel.editUserNickname(nickname)
            }
            dialog.setNegativeButton("取消") { dialog, which ->
                dialog.cancel()
            }
            dialog.show()
        }
    }

    override fun initViewMode() {
        viewModel.baseDataLiveData.observe(this, Observer { response ->
            handlerResponseLoading(response.status, response.statusTip)
            if (response.status != HttpResponseStatus.Status.SUCCESS)
                return@Observer
            toast(response.response?.msg ?: "修改成功")
            text_profile_user_name.text = nickname
        })
    }


}